
//
//  MyContactsCell.m
//  绿地管理系统
//
//  Created by rimi on 16/1/25.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "MyContactsCell.h"
#import "UIImageView+WebCache.h"
#import "relationTableViewCell.h"

@interface MyContactsCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIImageView *photoView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *genderView;
@property (nonatomic, strong) UITableView *relationTableView;
@property (nonatomic, strong) CAShapeLayer *lineLayer;

@property (nonatomic, strong) UIButton *arrowButton;

@end

@implementation MyContactsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self initializeAppearance];
    }
    return self;
}

- (void)initializeAppearance
{
    [self.contentView.layer addSublayer:self.lineLayer];
    [self.contentView addSubview:self.arrowButton];
    [self.contentView addSubview:self.photoView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.genderView];
    [self.contentView addSubview:self.relationTableView];
}

- (void)layoutSubviews
{
    CGFloat cellHeight = CGRectGetHeight(self.contentView.frame);
    CGFloat lineY = cellHeight - CGRectGetHeight(self.arrowButton.frame)/2;
    self.arrowButton.center = CGPointMake(350, lineY);
    
    UIBezierPath * linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(0, lineY)];
    [linePath addLineToPoint:CGPointMake(CGRectGetWidth(self.contentView.frame), lineY)];
    
    self.lineLayer.path = linePath.CGPath;
    
    
    CGFloat top = 38;
    CGFloat bottom = 25;
    self.relationTableView.frame = CGRectMake(self.relationTableView.frame.origin.x, 38, self.relationTableView.frame.size.width, cellHeight - top - bottom);
}
#pragma mark - callback
- (void)onArrowButton:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (self.cellButtonAction) {
        self.cellButtonAction(self,sender);
    }
}

#pragma mark - setter

- (void)setPhotoUrl:(NSString *)photoUrl
{
    _photoUrl = photoUrl;
    // 异步加载图片
    [self.photoView sd_setImageWithURL:[NSURL URLWithString:photoUrl]];
}

- (void)setName:(NSString *)name
{
    _name = name;
    self.nameLabel.text = name;
}

- (void)setGender:(id)gender
{
    _gender = gender;
}

- (void)setRelationList:(NSArray *)relationList
{
    _relationList = relationList;
    [self.relationTableView reloadData];
}


#pragma mark - getter
- (UIImageView *)photoView
{
    if (!_photoView) {
        _photoView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 50, 50)];
        
        UIImage * image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"face.jpg"]];
        _photoView.layer.cornerRadius = 25;
        _photoView.layer.masksToBounds = YES;
        _photoView.image = image;
    }
    return _photoView;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 10, 200, 18)];
        _nameLabel.font = [UIFont boldSystemFontOfSize:16];
        
    }
    return _nameLabel;
}

- (UIImageView *)genderView
{
    if (!_genderView) {
        _genderView = [[UIImageView alloc] initWithFrame:CGRectMake(75, 10, 22, 21)];
        _genderView.image = [UIImage imageNamed:@"男大图_03.png"];
        
    }
    return _genderView;
}

- (UIButton *)arrowButton
{
    if (!_arrowButton) {
        _arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _arrowButton.frame = CGRectMake(0, 0, 25, 25);
        [_arrowButton setImage:[UIImage imageNamed:@"2_03"] forState:UIControlStateNormal];
        [_arrowButton setImage:[UIImage imageNamed:@"1_03"] forState:UIControlStateSelected];
        [_arrowButton addTarget:self action:@selector(onArrowButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _arrowButton;
}

- (CAShapeLayer *)lineLayer
{
    if (!_lineLayer) {
        _lineLayer = [CAShapeLayer layer];
        _lineLayer.lineWidth = 1;
        _lineLayer.strokeColor = [UIColor grayColor].CGColor;
        _lineLayer.fillColor = [UIColor clearColor].CGColor;
        _lineLayer.backgroundColor = [UIColor clearColor].CGColor;
    }
    return _lineLayer;
}

- (UITableView *)relationTableView
{
    if (!_relationTableView) {
        _relationTableView = [[UITableView alloc] initWithFrame:CGRectMake(75, 38, 292, 59) style:UITableViewStylePlain];
        _relationTableView.dataSource = self;
        _relationTableView.delegate = self;
        [_relationTableView registerClass:[relationTableViewCell class] forCellReuseIdentifier:@"tableCellIdf"];
        _relationTableView.rowHeight = 30;
        _relationTableView.scrollEnabled = NO;
        _relationTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _relationTableView;
}
// dataSource方法写到了cell里面
// 实现dataSource方法唯一的方式有两个：1、cell自己做数据解析；2、cell让viewModel做数据解析
// 1、排除，是因为view本身不应该做数据解析的操作
// 2、排除，是因为view不能直接引用model相关的任何东西
// dataSource方法一旦写到cell里面，绝对不能满足MVVM的规范

// 如果想要强行用MVVM来实现，只能另外找一个地方实现dataSource。controller
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 是否是展开的情况
    // 如果不是展开的情况
    
    // 通过arrowButton的selected状态来判断是否展开
    if (self.arrowButton.selected) {
        return [self.relationList count];
    } else {
        if (self.relationList.count < 2) {
            return self.relationList.count;
        } else {
            return 2;
        }
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    relationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"tableCellIdf" forIndexPath:indexPath];
    NSDictionary * infoDic = self.relationList[indexPath.row];
    
    cell.real_nameLabel.text = infoDic[@"real_name"];
//    NSLog(@"%ld====================",[infoDic[@"relationship"] integerValue]);
    cell.relation_type.text = [NSString stringWithFormat:@"的%@", infoDic[@"relation_type"]];
    switch ([infoDic[@"relationship"] integerValue]) {
        case 1:
            cell.relationshipimageView.image = [UIImage imageNamed:@"非常重要_03"];
            cell.relationshipimageView1.image = [UIImage imageNamed:@"非常亲密_03"];
            cell.relationshipLabel.text = @"非常重要";
            cell.relationshipLabel1.text = @"非常亲密";
            break;
        case 2:
            cell.relationshipimageView.image = [UIImage imageNamed:@"不重要_03"];
            cell.relationshipimageView1.image = [UIImage imageNamed:@"不亲密_03"];
            cell.relationshipLabel.text = @"不重要";
            cell.relationshipLabel1.text = @"不亲密";
            break;
        case 3:
            cell.relationshipimageView.image = [UIImage imageNamed:@"一般重要_03"];
            cell.relationshipimageView1.image = [UIImage imageNamed:@"一般亲密_03"];
            cell.relationshipLabel.text = @"一般重要";
            cell.relationshipLabel1.text = @"一般亲密";
            break;
        case 4:
            cell.relationshipimageView.image = [UIImage imageNamed:@"重要_03"];
            cell.relationshipimageView1.image = [UIImage imageNamed:@"亲密_03"];
            cell.relationshipLabel.text = @"重要";
            cell.relationshipLabel1.text = @"亲密";
            break;
        case 5:
            cell.relationshipimageView.image = [UIImage imageNamed:@""];
            break;
            
        default:
            break;
    }

    
    return cell;
}



@end













